package com.unis.XmlNetty;

import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.concurrent.Executors;

import org.dom4j.DocumentException;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.group.DefaultChannelGroup;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.codec.serialization.ClassResolvers;
import org.jboss.netty.handler.codec.serialization.ObjectDecoder;
import org.jboss.netty.handler.codec.serialization.ObjectEncoder;

import com.unis.XmlNetty.beans.XmlPackge;
import com.unis.utils.Config;

public class byteToXmlServer {
	
	public static	ChannelGroup allChannel = new DefaultChannelGroup();
	public static	ChannelFactory factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
	public static	ServerBootstrap bootstrap = new ServerBootstrap(factory);	
	
	public static void run() throws Exception {
		// TODO Auto-generated method stub
		String serverport= Config.getConfigValue("/server//config-port");
		bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
		@Override
			public ChannelPipeline getPipeline() throws Exception {
				// TODO Auto-generated method stub
				ChannelPipeline pipeline = Channels.pipeline();
				pipeline.addLast("encoding", new ObjectEncoder());
				pipeline.addLast("decoding", new ObjectDecoder(ClassResolvers.cacheDisabled(XmlPackge.class.getClassLoader())));
				pipeline.addLast("handle", new byteToXmlServerHandle());			
				return pipeline;
			}
		});
		bootstrap.setOption("child.tcpNoDelay", true);    
        bootstrap.setOption("child.keepAlive", true);    
            
        Channel serverChannel = bootstrap.bind(new InetSocketAddress(Integer.parseInt(serverport)));
        allChannel.add(serverChannel);
        System.out.println("服务器启动");
	}
	
	public static void close(){		
		class test extends Thread {
			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					sleep(2000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				allChannel.close().awaitUninterruptibly();
				bootstrap.releaseExternalResources(); 
			}
		}
		Thread t = new test();
		t.start();
	}
	
	public static void getClient(){
		Iterator<Channel>iter=allChannel.iterator();
		while(iter.hasNext()){
		Channel ch = iter.next();
		System.out.print(ch+",");
		}
		System.out.println();
	}
	public static void main(String[] args) throws Exception {
		if(args.length ==1){			
			String url = args[0];
			Config.setUrl(url);
		}
		run();
	}
}
